﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
	/// <summary>
	/// 数据访问类:DeviceInfo
	/// </summary>
	public partial class DeviceInfo
	{
		public DeviceInfo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "DeviceInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from DeviceInfo");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(QunYing.Model.DeviceInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into DeviceInfo(");
			strSql.Append("名称,品牌,型号,箱体尺寸,鱼类品种,水草品种,水族箱地址,ICSA编号)");
			strSql.Append(" values (");
			strSql.Append("@名称,@品牌,@型号,@箱体尺寸,@鱼类品种,@水草品种,@水族箱地址,@ICSA编号)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@名称", SqlDbType.NVarChar,20),
					new SqlParameter("@品牌", SqlDbType.NVarChar,20),
					new SqlParameter("@型号", SqlDbType.NVarChar,50),
					new SqlParameter("@箱体尺寸", SqlDbType.NVarChar,50),
					new SqlParameter("@鱼类品种", SqlDbType.NVarChar,200),
					new SqlParameter("@水草品种", SqlDbType.NVarChar,200),
					new SqlParameter("@水族箱地址", SqlDbType.NVarChar,200),
					new SqlParameter("@ICSA编号", SqlDbType.NVarChar,20)};
			parameters[0].Value = model.名称;
			parameters[1].Value = model.品牌;
			parameters[2].Value = model.型号;
			parameters[3].Value = model.箱体尺寸;
			parameters[4].Value = model.鱼类品种;
			parameters[5].Value = model.水草品种;
			parameters[6].Value = model.水族箱地址;
			parameters[7].Value = model.ICSA编号;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(QunYing.Model.DeviceInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update DeviceInfo set ");
			strSql.Append("名称=@名称,");
			strSql.Append("品牌=@品牌,");
			strSql.Append("型号=@型号,");
			strSql.Append("箱体尺寸=@箱体尺寸,");
			strSql.Append("鱼类品种=@鱼类品种,");
			strSql.Append("水草品种=@水草品种,");
			strSql.Append("水族箱地址=@水族箱地址,");
			strSql.Append("ICSA编号=@ICSA编号");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@名称", SqlDbType.NVarChar,20),
					new SqlParameter("@品牌", SqlDbType.NVarChar,20),
					new SqlParameter("@型号", SqlDbType.NVarChar,50),
					new SqlParameter("@箱体尺寸", SqlDbType.NVarChar,50),
					new SqlParameter("@鱼类品种", SqlDbType.NVarChar,200),
					new SqlParameter("@水草品种", SqlDbType.NVarChar,200),
					new SqlParameter("@水族箱地址", SqlDbType.NVarChar,200),
					new SqlParameter("@ICSA编号", SqlDbType.NVarChar,20),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.名称;
			parameters[1].Value = model.品牌;
			parameters[2].Value = model.型号;
			parameters[3].Value = model.箱体尺寸;
			parameters[4].Value = model.鱼类品种;
			parameters[5].Value = model.水草品种;
			parameters[6].Value = model.水族箱地址;
			parameters[7].Value = model.ICSA编号;
			parameters[8].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DeviceInfo ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DeviceInfo ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public QunYing.Model.DeviceInfo GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,名称,品牌,型号,箱体尺寸,鱼类品种,水草品种,水族箱地址,ICSA编号 from DeviceInfo ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
			parameters[0].Value = ID;

			QunYing.Model.DeviceInfo model=new QunYing.Model.DeviceInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["名称"]!=null && ds.Tables[0].Rows[0]["名称"].ToString()!="")
				{
					model.名称=ds.Tables[0].Rows[0]["名称"].ToString();
				}
				if(ds.Tables[0].Rows[0]["品牌"]!=null && ds.Tables[0].Rows[0]["品牌"].ToString()!="")
				{
					model.品牌=ds.Tables[0].Rows[0]["品牌"].ToString();
				}
				if(ds.Tables[0].Rows[0]["型号"]!=null && ds.Tables[0].Rows[0]["型号"].ToString()!="")
				{
					model.型号=ds.Tables[0].Rows[0]["型号"].ToString();
				}
				if(ds.Tables[0].Rows[0]["箱体尺寸"]!=null && ds.Tables[0].Rows[0]["箱体尺寸"].ToString()!="")
				{
					model.箱体尺寸=ds.Tables[0].Rows[0]["箱体尺寸"].ToString();
				}
				if(ds.Tables[0].Rows[0]["鱼类品种"]!=null && ds.Tables[0].Rows[0]["鱼类品种"].ToString()!="")
				{
					model.鱼类品种=ds.Tables[0].Rows[0]["鱼类品种"].ToString();
				}
				if(ds.Tables[0].Rows[0]["水草品种"]!=null && ds.Tables[0].Rows[0]["水草品种"].ToString()!="")
				{
					model.水草品种=ds.Tables[0].Rows[0]["水草品种"].ToString();
				}
				if(ds.Tables[0].Rows[0]["水族箱地址"]!=null && ds.Tables[0].Rows[0]["水族箱地址"].ToString()!="")
				{
					model.水族箱地址=ds.Tables[0].Rows[0]["水族箱地址"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ICSA编号"]!=null && ds.Tables[0].Rows[0]["ICSA编号"].ToString()!="")
				{
					model.ICSA编号=ds.Tables[0].Rows[0]["ICSA编号"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,名称,品牌,型号,箱体尺寸,鱼类品种,水草品种,水族箱地址,ICSA编号 ");
			strSql.Append(" FROM DeviceInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,名称,品牌,型号,箱体尺寸,鱼类品种,水草品种,水族箱地址,ICSA编号 ");
			strSql.Append(" FROM DeviceInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "DeviceInfo";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

